

package com.hazelcast.jet.impl.execution;

import com.hazelcast.jet.impl.util.ProgressState;

import javax.annotation.Nonnull;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
import java.util.function.Consumer;

import static com.hazelcast.jet.impl.execution.DoneItem.DONE_ITEM;
import static com.hazelcast.jet.impl.util.ProgressState.*;

public class MockInboundStream implements InboundEdgeStream {
    private final int priority;
    private final Deque<Object> mockData;
    private final int chunkSize;
    private int ordinal;
    private SpecialBroadcastItem pendingItem = null;

    private boolean done;

    MockInboundStream(int priority, List<?> mockData, int chunkSize) {
        this.priority = priority;
        this.chunkSize = chunkSize;
        this.mockData = new ArrayDeque<>(mockData);
    }

    void push(Object... items) {
        mockData.addAll(Arrays.asList(items));
    }

    public void setOrdinal(int ordinal) {
        this.ordinal = ordinal;
    }

    @Nonnull
    @Override
    public ProgressState drainTo(@Nonnull Consumer<Object> dest) {
        if (done) {
            return WAS_ALREADY_DONE;
        }

        if (pendingItem != null) {
            dest.accept(pendingItem);
            pendingItem = null;
            return MADE_PROGRESS;
        }

        if (mockData.isEmpty()) {
            return NO_PROGRESS;
        }
        for (int i = 0; i < chunkSize && !mockData.isEmpty(); i++) {
            final Object item = mockData.poll();
            if (item == DONE_ITEM) {
                done = true;
                break;
            }
            if (item instanceof SpecialBroadcastItem broadcastItem) {
                if (i == 0) {
                    // if we meet special item first, just forward it and stop draining iteration.
                    dest.accept(item);
                } else {
                    // here, if we meet special item after normal items, stop draining iteration without skipping.
                    pendingItem = broadcastItem;
                }
                break;
            } else {
                dest.accept(item);
            }
        }
        return done ? DONE : MADE_PROGRESS;
    }

    @Override
    public boolean isDone() {
        return done;
    }

    @Override
    public int ordinal() {
        return ordinal;
    }

    @Override
    public int priority() {
        return priority;
    }

    public Deque<Object> remainingItems() {
        return mockData;
    }

    @Override
    public int sizes() {
        return mockData.size();
    }

    @Override
    public int capacities() {
        return Integer.MAX_VALUE;
    }
}
